{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fc65cf56",
   "metadata": {},
   "source": [
    "#### 第2章　感知机\n",
    "- 与门的实现\n",
    "- 或门的实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d222ae1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0)->0\n",
      "(1, 0)->0\n",
      "(0, 1)->0\n",
      "(1, 1)->1\n"
     ]
    }
   ],
   "source": [
    "# 与门的实现\n",
    "#np.array() 用于将输入数据转换为 NumPy 数组（ndarray）\n",
    "import numpy as np \n",
    "\n",
    "def AND(x1, x2):\n",
    "    x = np.array([x1,x2])\n",
    "    w = np.array([0.5,0.5])\n",
    "    b = -0.7\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <= 0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1 \n",
    "if __name__ == '__main__':\n",
    "    for x_num in [(0, 0), (1, 0), (0, 1), (1, 1)]:\n",
    "        y = AND(x_num[0], x_num[1])\n",
    "        print(str(x_num) + \"->\" +str(y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ce49159b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) ——> 0\n",
      "(1, 0) ——> 1\n",
      "(0, 1) ——> 1\n",
      "(1, 1) ——> 1\n"
     ]
    }
   ],
   "source": [
    "# 或门的实现\n",
    "import numpy as np \n",
    "def OR(x1 ,x2):\n",
    "    x = np.array([x1,x2])\n",
    "    w = np.array([0.5, 0.5])\n",
    "    b = -0.2\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <= 0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "if __name__ == '__main__':\n",
    "    for x_num in [(0, 0), (1, 0), (0, 1), (1, 1)]:\n",
    "        y = OR(x_num[0], x_num[1])\n",
    "        print(str(x_num)  + \" ——> \" + str(y))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cq_jupyter",
   "language": "python",
   "name": "cq_jupyter"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
